package com.edu.chongdian;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.edu.chongdian.model.Reward;

import java.util.List;

public class RewardAdapter extends RecyclerView.Adapter<RewardAdapter.RewardViewHolder> {
    private List<Reward> mRewards; // 奖励/商品列表
    private int mPoints; // 当前用户的积分数量

    public RewardAdapter(List<Reward> rewards) {
        mRewards = rewards;
    }

    @NonNull
    @Override
    public RewardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.reward_item, parent, false);
        return new RewardViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RewardViewHolder holder, int position) {
        Reward reward = mRewards.get(position);
        holder.bind(reward);
    }

    @Override
    public int getItemCount() {
        return mRewards.size();
    }

    public void setPoints(int points) {
        mPoints = points;
    }

    public int getmPoints() {
        notifyDataSetChanged();
        return mPoints;

    }

    class RewardViewHolder extends RecyclerView.ViewHolder {
        private TextView mNameTextView;
        private TextView mDescriptionTextView;
        private TextView mPointsTextView;
        private ImageView mImageView;
        private Button mRedeemButton;
        private TextView mNameTextView1;
        private TextView mDescriptionTextView1;
        private TextView mPointsTextView1;
        private ImageView mImageView1;
        private Button mRedeemButton1;

        RewardViewHolder(View itemView) {
            super(itemView);

            mNameTextView = itemView.findViewById(R.id.reward_name_text_view);
            mDescriptionTextView = itemView.findViewById(R.id.reward_description_text_view);
            mPointsTextView = itemView.findViewById(R.id.reward_points_text_view);
            mImageView = itemView.findViewById(R.id.reward_image_view);
            mRedeemButton = itemView.findViewById(R.id.redeem_button);
            mNameTextView1 = itemView.findViewById(R.id.reward_name);
            mDescriptionTextView1 = itemView.findViewById(R.id.reward_description);
            mPointsTextView1 = itemView.findViewById(R.id.reward_points);
            mImageView1 = itemView.findViewById(R.id.reward_image);
            mRedeemButton1 = itemView.findViewById(R.id.redeem);
            mRedeemButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && mPoints >= mRewards.get(position).getPoints()) {
                        mPoints -= mRewards.get(position).getPoints();
                        setPoints(mPoints);
                        // 发送奖励/商品给用户
                        // 更新积分数量
                        Log.e("TAG","剩余积分"+mPoints);
                        Log.e("TAG","get"+getmPoints());
                    }else{
                        mRedeemButton.setEnabled(false); // 积分不足，禁用按钮
                    }
                }
            });
        }
        void bind(Reward reward) {
            mNameTextView.setText(reward.getName());
            mDescriptionTextView.setText(reward.getDescription());
            mPointsTextView.setText(String.valueOf(reward.getPoints()));
            mImageView.setImageResource(reward.getImageResId());
            mNameTextView1.setText(reward.getName());
            mDescriptionTextView1.setText(reward.getDescription());
            mPointsTextView1.setText(String.valueOf(reward.getPoints()));
            mImageView1.setImageResource(reward.getImageResId());
        }
    }
}
